﻿/*******************************************************************************************
 *Copyright (c) 2017 Biobase All Rights Reserved.
 *CLR版本: 4.0.30319.18408
 *机器名称:3OWM56IB5STCV64
 *公司名称:Biobase
 *命名空间:NetMQDemoPublisher
 *文件名:Publisher
 *版本号:V1.0.0.0
 *唯一标识:4470b303-f7d7-4d48-ad8b-832c451422f0
 *当前的用户域:3OWM56IB5STCV64
 *创建人:mzyfbz
 *电子邮箱:mzyfbz@dingtalk.com
 *创建时间:2017-10-13 17:01:07

 *描述
 *
 *==========================================================================================
 *修改标记
 *修改时间:2017-10-13 17:01:07
 *修改人:mzyfbz
 *版本号:V1.0.0.0
 *描述:
 *
********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetMQ;
using NetMQ.Sockets;

namespace NetMQDemoPublisher
{
    public class Publisher:IPublisher
    {
        private object _lockObject = new object();

        private PublisherSocket _publisherSocket;

        public Publisher(string endPoint)
        {
            _publisherSocket = new PublisherSocket();
            _publisherSocket.Options.SendHighWatermark = 1000;
            _publisherSocket.Bind(endPoint);
        }
        #region Implementation of IDisposable

        /// <summary>
        /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
        /// </summary>
        public void Dispose()
        {
            lock (_lockObject)
            {
                _publisherSocket.Close();
                _publisherSocket.Dispose();
            }
        }

        /// <summary>
        /// 发布消息
        /// </summary>
        /// <param name="topicName">主题</param>
        /// <param name="data">内容</param>
        public void Publish(string topicName, string data)
        {
            lock (_lockObject)
            {
                _publisherSocket.SendMoreFrame(topicName).SendFrame(data);
            }
        }

        #endregion
    }
}
